package com.fany.filesearcher;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import com.fany.utils.CommonUtils;
import com.fany.utils.ConfigUtils;
import com.fany.utils.ContextUtils;

public class FileSearchThread implements Runnable
{
    private Context _context;
    private FileSearchHandler _loadHandler;

    public FileSearchThread(Context context, FileSearchHandler Handler)
    {
        _context = context;
        _loadHandler = Handler;
    }

    @Override
    public void run()
    {
        ArrayList<String> roots = ConfigUtils.getInstance().getSearchPaths();
        String[] strRoots = new String[roots.size()];
        for (int i = 0; i < roots.size(); i++)
        {
            strRoots[i] = roots.get(i);
        }
        FileScanner.instance().beginScan(strRoots);
        SQLiteFileRecorder recorder = new SQLiteFileRecorder(_context);
        try
        {
            recorder.clear();
            while (!FileScanner.instance().isCompleted())
            {
                List<String> records = FileScanner.instance().scanFiles(true);
                recorder.record(records);
            }
        }
        finally
        {
            FileScanner.instance().endScan();
        }
        _loadHandler.sendEmptyMessage(recorder.getRecordCount());
    }

    public void interrupt()
    {
        FileScanner.instance().interrupt();
    }
}